/**
 *
 */
package com.angel.mocks.data.generator.rowProcessorCommand;

import com.angel.io.exceptions.InvalidRowDataException;
import com.angel.io.parameters.ParametersService;
import com.angel.io.processors.commands.exports.ExportRowProcessorCommand;
import com.angel.io.processors.commands.imports.ImportRowProcessorCommand;
import com.angel.io.type.rows.ExportRowFile;
import com.angel.io.type.rows.ImportRowFile;
import com.angel.io.type.rows.impl.HeaderRowFile;
import com.angel.mocks.data.generator.domainObject.Address;

/**
 * @author William
 *
 */
public class AddressRowProcessorCommand implements ImportRowProcessorCommand, ExportRowProcessorCommand{

	public static final String NAME_COLUMN = "name";
	public static final String CITY_COLUMN = "city";
	public static final String NUMBER_COLUMN = "number";

	public void checkHeader(HeaderRowFile headerRowFile) throws InvalidRowDataException {
		if(!headerRowFile.hasAllColumnName(NAME_COLUMN, CITY_COLUMN, NUMBER_COLUMN)){
			throw new InvalidRowDataException("Header is invalid. AddressExcelRowProcessor expects has 'name, city, and number columns.'");
		}
	}

	public String[] getHeaderColumnsNames(){
		return new String[]{NAME_COLUMN, CITY_COLUMN, NUMBER_COLUMN};
	}

	public void checkRowData(ImportRowFile rowFile, HeaderRowFile header) throws InvalidRowDataException {
		String name = rowFile.getStringValue(header.getColumnPosition(NAME_COLUMN));
		String city = rowFile.getStringValue(header.getColumnPosition(CITY_COLUMN));
		Float number  = rowFile.getFloatValue(header.getColumnPosition(NUMBER_COLUMN));

		if(name == null || city == null || number == null ){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"Name: [" + name + "] - " +
					"City: [" + city + "] - " +
					"Number: [" + number + "] - ");
		}
	}

	@SuppressWarnings("unchecked")
	public <T> T processRow(ImportRowFile rowFile, HeaderRowFile header, ParametersService parametersServices) {

		String city = rowFile.getStringValue(header.getColumnPosition(CITY_COLUMN));
		String name = rowFile.getStringValue(header.getColumnPosition(NAME_COLUMN));
		Float number = rowFile.getFloatValue(header.getColumnPosition(NUMBER_COLUMN));

		Address address = new Address();
		address.setCity(city);
		address.setName(name);
		address.setNumber(number.intValue());

		return (T) address;
	}

	public void processRow(ExportRowFile exportRowFile, Object toExport, HeaderRowFile headerRowFile) throws InvalidRowDataException {
		Address addressToExport = (Address) toExport;
		exportRowFile.addStringValue(addressToExport.getCity(), headerRowFile.getColumnPosition(CITY_COLUMN));
		exportRowFile.addStringValue(addressToExport.getName(), headerRowFile.getColumnPosition(NAME_COLUMN));
		exportRowFile.addIntegerValue(addressToExport.getNumber(), headerRowFile.getColumnPosition(NUMBER_COLUMN));
	}


}
